package com.qingxin.wuye.repertory.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.repertory.entity.Gi;
import com.qingxin.wuye.repertory.entity.ResManage;

@Service
public class GiService extends BaseService<Gi, String>{
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object getQjdXltjData(String repertoryid){
		Map<String, Object> rsmap = new HashMap<>();
		List<ResManage> resManages = new ArrayList<ResManage>();
		List<String> categories = new ArrayList<>();
		List<Object> inNum = new ArrayList<>();
		List<Object> outNum = new ArrayList<>();
		List<Object> giNum = new ArrayList<>();
		List<Gi> gilist = new ArrayList<>();
		Querier querier1 = Querier.create("select new com.qingxin.wuye.repertory.entity.ResManage(" +
				"r.resid, r.typeid, r.resDm, r.resTm, r.resName, r.pinyin, r.model, r.unit," +
				" r.sccj, r.remark, r.stockMax, r.stockMin, r.inPrice, r.outPrice, r.middlePrice)" +
				" from ResManage r where 1 = 1");
		if (StringUtils.isNotEmpty(repertoryid)) {
			querier1.append(" and r.resid in (select g.resid from Gi g where g.repertoryid=:repertoryid) ");
			querier1.setParam("repertoryid",repertoryid);
		}
		querier1.append(" order by r.createTime desc");
		resManages = listByQuerier(querier1);
		
		if(resManages == null || resManages.size()==0){
			return new HashMap();
		}
		for(ResManage resManage : resManages){
			categories.add(resManage.getResName());
			gilist = listByHql("from Gi where repertoryid=? and resid = ?", repertoryid , resManage.getResid());
			if(gilist != null){
				for(Gi gi : gilist){
					inNum.add(gi.getInNum());
					outNum.add(gi.getOutNum());
					giNum.add(gi.getGinum());
				}
			}
			
		}
		rsmap.put("categories", categories);
		rsmap.put("inNum", inNum);
		rsmap.put("outNum", outNum);
		rsmap.put("giNum", giNum);
		return rsmap;
	}
	
	
	public Pager list(String searchname, String repertoryid ,PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.repertory.entity.Gi(" +
				"g.giid,g.repertoryid,g.resid,g.ginum,g.inNum,g.outNum,m.resName,t.name as repertoryName)" +
				" from Gi g, Repertory t ,ResManage m" +
				" where g.repertoryid = t.repertoryid and g.resid = m.resid");
		
		/*if(StringUtils.isNotEmpty(searchname)){
			searchname = searchname.replace("'", "");
			querier.append(" and name like :name ");
			querier.setParam("name", "%"+searchname+"%");
		}*/
		
		if(StringUtils.isNotEmpty(repertoryid)){
			querier.append(" and g.repertoryid = :repertoryid ");
			querier.setParam("repertoryid", repertoryid);
		}
		
		//querier.append(pageParam.getOrderby(" order by g.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	/**
	 * 添加商品的时候默认添加一条库存信息
	 * @param gi
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Gi gi, LoginUser loginUser, HttpServletRequest request){
		this.save(gi);
		return null;
	}
	
	/**
	 * 更新库存
	 * @param gi
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Gi gi, LoginUser loginUser, HttpServletRequest request){
		Gi old = getSafeEntity(gi.getGiid());
		String fields = "ginum,inNum,outNum";
		BeanUtil.setTargetFields(old, gi, fields, true);
		Updater<Gi> updater = new Updater<>(old, fields);
		this.update(updater);
		return null;
	}
	
	
	public Object delete(String resid, String repertoryid,LoginUser loginUser, HttpServletRequest request){
		Gi gi = (Gi) getByHql("from Gi where repertoryid=? and resid=?",repertoryid, resid);
		if(gi==null){
			return null;
		}
		this.delete(gi);
		return null;
	}
	
}
